//
//  PainterIndexQKT.m
//  ChartRefactor
//
//  Created by CaoJun on 15/9/14.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "PainterIndexQKT.h"
#import "QKT.h"

using namespace std;
using namespace ytx;

@implementation PainterIndexQKT

- (void)__attribute__((annotate("oclint:suppress[long method]"))) __attribute__((annotate("oclint:suppress[high ncss method]")))drawRect:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    Quotation * data = indexPosition->getData();
    
    vector<std::vector<double> > & indexData = data->indexData;
    
    if (indexData.size() == 0 || indexData[0].size() == 0) {
        return;
    }
    
    std::vector<double> & qk = indexData[0];
    
    string backgroundColor = "", kColor = "", triangleColor = "";
    float zoomX, tempX = rect.getBeginX(), halfX = rect.getBeginX(), beginY = rect.getBeginY(), height = rect.getRectHeight();
    
    int startIndex = indexPosition->getStartDataIndex(), endIndex = indexPosition->getEndDataIndex();
    
    zoomX = rect.getZoomX();
    
    halfX += zoomX * 0.5;
    
    QKTType::QKTType value, preValue = QKTType::Flat;

    size_t index;
    
    KLineItem item;
    
    float HighY, LowY, BeginY, EndY, UpY, DownY;
    float lineWidth = style.quoteLineWidth, lineWidthHalf = lineWidth * 0.5f;
    
    [ctx setLineWidth: lineWidth];
    for (std::vector<double>::iterator it = qk.begin() + startIndex; it != qk.begin() + endIndex; ++it) {
        
        value = (QKTType::QKTType)(*it);
        index = it-qk.begin();
        
        item = data->kLineData[index];
        
        BeginY = rect.getPxY(item.open);
        EndY = rect.getPxY(item.close);
        HighY = rect.getPxY(item.high);
        LowY = rect.getPxY(item.low);
        
        switch (value) {
            case QKTType::Long:
                backgroundColor = style.QKTypeBackgroundLong;
                kColor = style.QKTypeLong;
                [ctx fillRectWithX:tempX withY:beginY withWidth:zoomX withHeight:height withFillColor: [NSString stringWithUTF8String:backgroundColor.c_str()] ];
                break;
            case QKTType::Short:
                backgroundColor = style.QKTypeBackgroundShort;
                kColor = style.QKTypeShort;
                [ctx fillRectWithX:tempX withY:beginY withWidth:zoomX withHeight:height withFillColor: [NSString stringWithUTF8String:backgroundColor.c_str()] ];
                break;
            case QKTType::Flat:
                backgroundColor = style.QKTypeBackgroundFlat;
                kColor = style.QKTypeFlat;
                break;
        }

        preValue = value;
        
        //画K线
        [ctx setStrokeStyle: [NSString stringWithUTF8String:kColor.c_str()] ];
        if(item.open > item.close)
        {
            UpY = BeginY;
            DownY = EndY;
            [ctx fillRectWithX1:tempX + lineWidthHalf withY1:EndY withX2:tempX + zoomX - lineWidthHalf withY2:BeginY withFillColor:[NSString stringWithUTF8String:kColor.c_str()]];
        }
        else{//涨的画空心K线
            UpY = EndY;
            DownY = BeginY;
            [ctx drawLineWithX1:tempX + lineWidth withY1:BeginY withX2:tempX + lineWidth withY2:EndY];
            [ctx drawLineWithX1:tempX + lineWidth withY1:EndY withX2:tempX + zoomX - lineWidth withY2:EndY];
            [ctx drawLineWithX1:tempX + zoomX - lineWidth withY1:EndY withX2:tempX + zoomX - lineWidth withY2:BeginY];
            [ctx drawLineWithX1:tempX + zoomX - lineWidth withY1:BeginY withX2:tempX + lineWidth withY2:BeginY];
        }
        
        [ctx drawLineWithX1:halfX withY1:HighY withX2:halfX withY2:UpY];
        [ctx drawLineWithX1:halfX withY1:LowY withX2:halfX withY2:DownY];
        
        tempX += zoomX;
        halfX += zoomX;
    }
}


@end
